<template>
  <div>
    <el-card>
      <template #header>
        <h4>订单管理</h4>
      </template>
      <div>
        <!-- 查询 -->
        <el-form :inline="true" :model="orderFrom" class="demo-form-inline">
          <el-form-item label="订单号" size="mini">
            <el-input
              v-model="orderFrom.orderNo"
              placeholder="订单号"
            ></el-input>
          </el-form-item>
          <el-form-item label="收货人" size="mini">
            <el-input
              v-model="orderFrom.consignee"
              placeholder="收货人"
            ></el-input>
          </el-form-item>
          <el-form-item label="手机号" size="mini">
            <el-input v-model="orderFrom.phone" placeholder="手机号"></el-input>
          </el-form-item>
          <el-form-item label="订单状态" size="mini">
            <el-select v-model="orderFrom.orderState" placeholder="订单状态">
              <el-option label="已受理" value="已受理"></el-option>
              <el-option label="派送中" value="派送中"></el-option>
              <el-option label="已完成" value="已完成"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="选择时间" size="mini">
            <el-date-picker
              v-model="orderFrom.date"
              type="datetimerange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item size="mini">
            <el-button type="primary" @click="onSubmit('search')" size="mini"
              >查询</el-button
            >
          </el-form-item>
        </el-form>

        <!-- 表格 -->
        <el-table :data="tableData" border style="width: 1146px">
          <el-table-column fixed prop="orderNo" label="订单号" width="80">
          </el-table-column>
          <el-table-column label="下单时间" width="160">
            <template v-slot="{ row }">
              {{ row.orderTime | formatDate }}
            </template>
          </el-table-column>
          <el-table-column prop="phone" label="手机号" width="120">
          </el-table-column>
          <el-table-column prop="consignee" label="收货人" width="120">
          </el-table-column>
          <el-table-column prop="deliverAddress" label="配送地址" width="200">
          </el-table-column>
          <el-table-column prop="remarks" label="用户备注" width="120">
          </el-table-column>
          <el-table-column prop="orderAmount" label="订单金额" width="120">
          </el-table-column>
          <el-table-column prop="orderState" label="订单状态" width="120">
          </el-table-column>
          <el-table-column fixed="right" label="操作" width="100">
            <template slot-scope="scope">
              <el-button
                @click="handleClick(scope.row)"
                type="text"
                size="small"
                >查看</el-button
              >
              <el-button type="text" size="small">编辑</el-button>
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页 -->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[5, 10, 20, 50]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>

        <!-- 查看 -->
        <el-dialog title="配送信息" :visible.sync="dialogVisible" width="30%">
          <el-timeline :reverse="reverse">
            <el-timeline-item
              v-for="(activity, index) in activities"
              :key="index"
              :timestamp="activity.timestamp | formatDate"
            >
              {{ activity.content }}
            </el-timeline-item>
          </el-timeline>
          <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="dialogVisible = false"
              >确 定</el-button
            >
          </span>
        </el-dialog>
      </div>
    </el-card>
  </div>
</template>

<script>
import { orderList, search } from '../../api/info.js'
export default {
  data() {
    return {
      tableData: [],
      orderFrom: {
        orderNo: '',
        consignee: '',
        phone: '',
        orderState: '',
        date: ''
      },
      total: 0,
      currentPage: 1,
      pageSize: 5,
      dialogVisible: false,
      reverse: true,
      activities: [
        {
          content: '下单时间',
          timestamp: '2018-04-15'
        },
        {
          content: '送达时间',
          timestamp: '2018-04-13'
        }
      ]
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 获取列表
    async getList() {
      const { data, total } = await orderList({
        currentPage: this.currentPage,
        pageSize: this.pageSize,
        orderNo: '',
        consignee: '',
        phone: '',
        orderState: '',
        date: ''
      })
      console.log(data)
      this.total = total
      this.tableData = data
      this.onSubmit()
    },

    handleClick(row) {
      this.dialogVisible = true
      console.log(row)
      this.activities[0].timestamp = row.orderTime
      this.activities[1].timestamp = row.deliveryTime
    },

    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.pageSize = val
      this.getList()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.currentPage = val
      this.getList()
    },

    // 查询
    async onSubmit(s) {
      if (this.orderFrom.date !== '') {
        this.orderFrom.date = JSON.stringify(this.orderFrom.date)
      }
      if (s) {
        // 从第一页开始查询
        this.currentPage = 1
      }

      const { data, total } = await search({
        currentPage: this.currentPage,
        pageSize: this.pageSize,
        ...this.orderFrom
      })
      console.log(data)
      this.total = total
      this.tableData = data
    }
  }
}
</script>

<style lang="less" scoped>
.el-table {
  margin: 20px 0;
}
.el-timeline-item {
  margin-left: 60px;
}
</style>
